package org.pragma;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.pragma.objects.PragmaNumberClass;
import org.pragma.objects.PragmaObject;
import org.pragma.objects.PragmaString;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Pragma extends PragmaBlockOld{

	public static void main(String[] args) {
		
		Pragma p = new Pragma();
		try {
			p.execute();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void execute() throws Exception{

		PragmaContext context = new PragmaContext();
		createClasses(context);

		Document doc = loadXmlFile("Person.pragma.xml");
		PragmaBlock method = new PragmaBlock().load(doc.getDocumentElement());
		
		//loadUserCode();
		//loadPrimitiveTestCode();
		System.out.println(display(""));
		System.out.println("execute-------------");
		//execute(context);


	}
	
	private void loadPrimitiveTestCode() {
		rootCall("createClass");
	}

	PragmaCall rootCall(String method){
		return new PragmaCall().setCalle(new PragmaReference().setRootObject()).setMethod(method);
		
	}
	private void loadUserCode() {
		add(new PragmaCall()
			.setCalle(new PragmaReference().setRootObject())
			.setMethod("declareClass")
			.addParameter(
				new PragmaClass("Person")
					.setConstructorMethod(new PragmaBlock("new")
						.addParameter(new PragmaParameter("this"))
						.add(new PragmaCall()
							.setCalle(new PragmaReference().setRootObject())
							.setMethod("assign")
							.addParameter(new PragmaReference("age").setFromThis())
							.addParameter(new PragmaCall()
								.setCalle(new PragmaReference().setRootObject())
								.setMethod("createInstance")
								.addParameter(new PragmaString("number")))
						)
					)
					.add(new PragmaBlock("getAge")
						.add(new PragmaCall()
							.setCalle(new PragmaReference().setRootObject())
							.setMethod("assign")
							.addParameter(new PragmaReference("return"))
							.addParameter(new PragmaReference("age").setFromThis())
						)
					)
					.add(new PragmaBlock("spendYear")
						.addParameter(new PragmaParameter("nbYear"))
							.add(new PragmaCall()
								.setCalle(new PragmaReference("age").setFromThis())
								.setMethod("add")
								.addParameter(new PragmaCall()
									.setCalle(new PragmaReference().setRootObject())
									.setMethod("createInstance")
									.addParameter(new PragmaString("number"))
									.addParameter(new PragmaString("1"))
								)
						)
					)
			)
		)
		/*.add(new PragmaCall()
			.setCalle(new PragmaReference().setRootObject())
			.setMethod("declare")
			.addParameter(new PragmaString("p"))
		)*/
		.add(new PragmaCall()
			.setCalle(new PragmaReference().setRootObject())
			.setMethod("assign")
			.addParameter(new PragmaReference("p"))
			.addParameter(new PragmaCall()
				.setCalle(new PragmaReference().setRootObject())
				.setMethod("createInstance")
				.addParameter(new PragmaString("Person"))
			)
		)
		.add(new PragmaCall()
			.setCalle(new PragmaReference("p"))
			.setMethod("spendYear")
			.addParameter(new PragmaNumberClass("5"))
		)
		.add(new PragmaCall()
			.setCalle(new PragmaReference().setRootObject())
			.setMethod("print")
			.addParameter(
				new PragmaCall()
				.setCalle(new PragmaReference("p"))
				.setMethod("getAge")
			)
		)
		;
		
	}

	void createClasses(PragmaContext context) {
		PragmaObject po = new PragmaObject().setClassObject(PragmaRootClass.getInstance());
		context.set("root".intern(),po);
		context.setCtxTitle("baseContext");
		context.setClass("number",new PragmaClass("number")
			.setConstructorMethod(new PragmaBlock("new")
				.addParameter(new PragmaParameter("this"))
			)	
			.add(new PragmaBlock("add")
				.addParameter(new PragmaParameter("number"))
			)
		);
	}
	
	Document loadXmlFile(String address){
		Document result=null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			result = db.parse(new File(address));
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
		
	}

}
